package com.iotimc.attendancesystem.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.iotimc.attendancesystem.R;

import java.util.ArrayList;
import java.util.List;

public class IpEditText extends LinearLayout implements TextWatcher {
    private static final String TAG = "IPEditText";
    private int width;
    private int height;
    private Paint paint;
    private static final int DEFAULT_TEXT_MAX_LENGTH = 3;
    private static final int DEFAULT_TEXT_SIZE = 16;
    private static final int DEFAULT_TEXT_COLOR = Color.BLACK;
    private static final int DEFAULT_BORDER_COLOR = Color.BLACK;
    private static final int DEFAULT_BORDER_WIDTH = 2;
    private static final int DEFAULT_POINT_COLOR = Color.BLACK;
    private static final int DEFAULT_POINT_WIDTH = 5;
    private static final int DEFAULT_IP_EDITTEXT_LENGTH = 4;
    private int textLength;
    private int textSize;
    private int textColor;
    private int borderColor;
    private int contentBackColor;
    private int borderWidth;
    private int pointColor;
    private int pointWidth;
    private int editNumber;
    private int default_height = px2dp(65);
    private int default_width = px2dp(80);
    private List<EditText> data = new ArrayList<>();

    private TextChangeListener textChangeListener;

    public IpEditText(Context context) {
        this(context, null);
    }

    public IpEditText(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IpEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.IpEditText, defStyleAttr, 0);
        textLength = ta.getInt(R.styleable.IpEditText_textLength, DEFAULT_TEXT_MAX_LENGTH);

        textSize = (int) ta.getDimension(R.styleable.IpEditText_textSize, DEFAULT_TEXT_SIZE);
        textColor = ta.getColor(R.styleable.IpEditText_textColor, DEFAULT_TEXT_COLOR);

        borderColor = ta.getColor(R.styleable.IpEditText_borderColor, DEFAULT_BORDER_COLOR);
        borderWidth = (int) ta.getDimension(R.styleable.IpEditText_borderWidth, DEFAULT_BORDER_WIDTH);

        contentBackColor = ta.getColor(R.styleable.IpEditText_borderContentColor, DEFAULT_BORDER_COLOR);

        pointColor = ta.getColor(R.styleable.IpEditText_pointColor, DEFAULT_POINT_COLOR);
        pointWidth = (int) ta.getDimension(R.styleable.IpEditText_pointWidth, DEFAULT_POINT_WIDTH);

        editNumber = ta.getInt(R.styleable.IpEditText_editNumber, DEFAULT_IP_EDITTEXT_LENGTH);

        init(context);
        initPaint();
    }

    public float getTextSize() {
        return data.get(0).getTextSize();
    }

    private void initPaint() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(2);
        paint.setStyle(Paint.Style.FILL);
    }


    /**
     * 初始化editText
     * @param context
     */
    private void init(Context context) {
        for (int i = 0; i < editNumber; i++) {
            EditText edit = new EditText(context);
            edit.setBackground(null);
            edit.setFilters(new InputFilter[]{new InputFilter.LengthFilter(textLength)});
            edit.setTextSize(textSize);
            edit.setBackgroundColor(Color.argb(0,0,0,0));
            edit.setTextColor(textColor);
            edit.setGravity(Gravity.CENTER);
            edit.setInputType(InputType.TYPE_CLASS_TEXT);
            //edit.setInputType(InputType.TYPE_CLASS_NUMBER);
            //int inputback = edit.getInputType();
            //edit.setInputType(InputType.TYPE_NULL);
            //edit.setInputType(inputback);
            edit.setOnTouchListener(onTouchListener);
            edit.setMinHeight(default_height);
            edit.setMinWidth(default_width);
            edit.setTag(i);
            edit.setMaxLines(1);
            edit.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1));
            edit.addTextChangedListener(this);
            addView(edit);
            data.add(edit);
        }

        setDividerDrawable(getResources().getDrawable(android.R.drawable.divider_horizontal_textfield));
        setOrientation(LinearLayout.HORIZONTAL);
    }





    /**
     * touchListener 点击是事件
     */
    OnTouchListener onTouchListener = new OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            String content = ((EditText)view).getText().toString();
            if(content!=null) {
                ((EditText) view).setSelection(content.length());
            }
            view.requestFocus();

            return true;
        }
    };









    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int l = data.get(0).getLeft();
        int t = data.get(0).getTop() - getPaddingTop();
        int r = width - getPaddingRight();
        int b = height;
        Rect rect = new Rect(l, t, r, b);
        paint.setColor(borderColor);
        paint.setStrokeWidth(borderWidth);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawRect(rect, paint);

        Rect rectcontent = new Rect(l+borderWidth/2, t+borderWidth/2, r-borderWidth/2, b-borderWidth/2);
        paint.setColor(contentBackColor);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawRect(rectcontent, paint);

        int y = height / 2;
        int x = width / editNumber;
        paint.setStrokeWidth(pointWidth);
        paint.setColor(pointColor);
        for (int i = 1; i < data.size(); i++) {
            canvas.drawPoint(x * i, y, paint);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (textChangeListener != null) {

            textChangeListener.onTextChangeListener(true);
        }
        if (listener != null) {
            listener.afterTextChanged(getSuperEditTextValue());
        }
        if (s.length() == 3) {
            for (int i = 0; i < data.size(); i++) {
                EditText edit = data.get(i);
                String val = edit.getText().toString();
                if (val.length() == 0) {
                    edit.requestFocus();
                    return;
                }
            }
        } else if (s.length() == 0) {
            for (int i = data.size() - 1; i >= 0; i--) {
                EditText edit = data.get(i);
                edit.setFocusable(true);
                String val = edit.getText().toString();
                if (val.length() == 3) {
                    edit.requestFocus();
                    edit.setSelection(3);
                    return;
                }else if(val.length() > 0) {
                    edit.requestFocus();
                    edit.setSelection(val.length());
                    return;
                }
            }
        }
    }

    private String[] getSuperEditTextValue() {
        String[] val = new String[editNumber];
        for (int i = 0; i < editNumber; i++) {
            val[i] = data.get(i).getText().toString();
            Log.d(TAG, data.get(i).getText().toString());
        }
        return val;
    }


    /**
     * 获取editText值
     * @return
     */
    public String getValue(){
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < editNumber; i++) {
            String value = data.get(i).getText().toString().replaceAll("^(0+)", "").trim();
            if (value != null && "".equalsIgnoreCase(value)) {

                value = "0";
            }
            stringBuilder.append(value);
            if (i < 3) {

                stringBuilder.append(".");
            }
        }
        return stringBuilder.toString();
    }


    public void setValue(String ipValue) {
        if (ipValue != null) {
            String[] strings = ipValue.split("\\.");
            for (int i = 0; i < strings.length; i++) {
                data.get(i).clearFocus();
                data.get(i).setText(strings[i]);
            }
        }

        if (textChangeListener != null) {

            textChangeListener.onTextChangeListener(false);
        }
    }


    private void setSuperEdittextValue(String[] s) {
        for (int i = 0; i < s.length; i++) {
            data.get(i).setText(s[i]);
        }
    }

    public boolean getSuperCompile() {
        for (int i = 0; i < editNumber; i++) {
            String str = data.get(i).getText().toString();
            if (Integer.parseInt(str) <= 255) {
                return true;
            }
        }
        return false;
    }

    public int px2dp(int val) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, val, getResources().getDisplayMetrics());
    }

    public int px2sp(int val) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, val, getResources().getDisplayMetrics());
    }

    public interface SuperTextWatcher {
        public void afterTextChanged(String[] s);
    }

    private SuperTextWatcher listener;

    public void setSuperTextWatcher(SuperTextWatcher listener) {
        this.listener = listener;
    }



    public interface TextChangeListener{
        void onTextChangeListener(boolean change);
    }

    public void setTextChangeListener(TextChangeListener textChangeListener) {
        this.textChangeListener = textChangeListener;
    }

}